package com.zj.androidcamera.activity;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Message;
import android.util.Size;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.ImageView;

import com.zj.androidcamera.R;
import com.zj.androidcamera.bean.FaceBean;
import com.zj.androidcamera.bean.ImageFrame;
import com.zj.androidcamera.thread.FaceThread;
import com.zj.androidcamera.util.MatrixUtil;
import com.zj.androidcamera.view.FaceView;
import com.zj.util.CameraUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PreviewActivity extends BaseActivity
        implements SurfaceHolder.Callback, Camera.PreviewCallback,
        Camera.FaceDetectionListener, FaceThread.FaceInterface {

    @BindView(R.id.sv_preview)
    SurfaceView svPreview;
    @BindView(R.id.fv_face)
    FaceView fvFace;
    @BindView(R.id.iv_face)
    ImageView ivFace;

    // 关于camera的操作都在这里
    private CameraUtil cameraUtil = CameraUtil.getInstance();
    // 预览管理
    private SurfaceHolder surfaceHolder;
    // 保存预览图像
    private List<ImageFrame> frameList = Collections.synchronizedList(new ArrayList<ImageFrame>());
    // 人脸处理程序
    private FaceThread faceThread;

    //预览图像大小
    private Size previewSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview);
        ButterKnife.bind(this);

        cameraUtil.init();

        surfaceHolder = svPreview.getHolder();
        surfaceHolder.addCallback(this);

        faceThread = new FaceThread();
        faceThread.setFaceInterface(this);
        faceThread.start();
    }

    @Override
    public void onFaceDetection(Camera.Face[] faces, Camera camera) {
        fvFace.setFaces(faces);
        synchronized (frameList) {
            if (frameList.size() != 0 && faces.length != 0) {
                Rect rect = getDetectArea(faces[0]);
                ImageFrame frame = frameList.get(0);
                FaceBean bean = new FaceBean(getDetectArea(faces[0]), frame.width,
                        frame.height, frame.data);

                Message msg = Message.obtain(faceThread.handler, 0, bean);
                msg.sendToTarget();
            }
        }
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        synchronized (frameList) {
            frameList.add(new ImageFrame(data, previewSize.getWidth(), previewSize.getHeight()));
            if (frameList.size() > 5) {
                ImageFrame frame = frameList.get(0);
                frameList.remove(0);
                frame.data = null;
                frame = null;
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        cameraUtil.initPreview(surfaceHolder, svPreview.getWidth(), svPreview.getHeight(), this);
        cameraUtil.initFaceDetect(this);

        previewSize = cameraUtil.getPreviewSize();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        cameraUtil.release();
    }

    @OnClick(R.id.sv_preview)
    public void onViewClicked() {
        cameraUtil.autoFocus(null);
    }

    @Override
    public void handleFace(int[] pixels, int width, int height) {
        final Bitmap bmp = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);

        ivFace.post(new Runnable() {
            @Override
            public void run() {
                ivFace.setImageBitmap(bmp);
            }
        });
    }

    private Rect getDetectArea(Camera.Face face) {
        Size faceViewSize = fvFace.getViewSize();
        Rect rect = cameraUtil.getFaceRect(face, faceViewSize);
        return MatrixUtil.matrixTransformation(faceViewSize, rect, previewSize);
    }
}
